package com.cskaoyan.service.front.user;


import com.cskaoyan.bean.po.*;
import com.cskaoyan.bean.vo.front.user.OrderStateVo;
import com.cskaoyan.mapper.MarketOrderGoodsMapper;
import com.cskaoyan.mapper.MarketOrderMapper;
import com.cskaoyan.mapper.MarketSystemMapper;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.util.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * impl wx用户服务
 *
 * @author 李然
 * @date 2022/10/14
 */
@Service
public class WXUserServiceImpl implements WXUserService {
    @Autowired
    MarketSystemMapper systemMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    MarketOrderMapper orderMapper;

    @Autowired
    MarketOrderGoodsMapper orderGoodsMapper;

    /**
     * 通过用户名 进行多表查询 查询出该用户所有订单所在不同状态的数量
     *
     * @return 不同订单状态的数量
     */
    @Override
    public OrderStateVo getUserIndex() {

////        获取用户名
//
//        String username = (String) SecurityUtils.getSubject().getSession().getAttribute("username");
////        if (username == null || "".equals(username)) {
////            return null;
////        }
//        if(!ParamUtils.isValid(username)) {
//            return null;
//        }
//        MarketUserExample userExample = new MarketUserExample();
//        userExample.createCriteria().andUsernameEqualTo(username);
//
//        // TODO: 2022/10/14 是否需要静态注入
//        /**
//         * solution:
//         *      private static UserMapper staticUserMapper;
//         *      staticUserMapper = this.userMapper;
//         */
//        List<MarketUser> users = userMapper.selectByExample(userExample);
//
//        //拿到具体用户的登录信息
//        MarketUser marketUser = users.get(0);
        MarketUser marketUser = SessionUtil.getUserInfo();

        //判断是否为登录状态

        if(marketUser == null) {
            return new OrderStateVo(0, 0, 0, 0);
        }

//        获取用户ID
        Integer userId = marketUser.getId();

        //查询待付款的订单数
        MarketOrderExample orderExample = new MarketOrderExample();
        MarketOrderExample.Criteria orderCriteria = orderExample.createCriteria();
        orderCriteria.andDeletedEqualTo(false);
        orderCriteria.andUserIdEqualTo(userId);

        List<MarketOrder> orders = orderMapper.selectByExample(orderExample);

        //待收货订单数
        int unrecv = 0;
        //待评价订单数
        int uncomment = 0;
        //待付款订单数
        int unpaid = 0;
        //待发货订单数
        int unship = 0;


        //遍历该用户的订单
        for (MarketOrder order : orders) {

            if (userId.equals(order.getUserId())) {

                //判断是否是未付款状态
                if (101 == order.getOrderStatus()) {
                    //添加时间
                    Date addTime = order.getAddTime();
                    MarketSystem system = systemMapper.selectByPrimaryKey(1);
                    // 获得超时时间(分钟)
                    String keyValue = system.getKeyValue();

                    //将超时时间转换为分钟
                    long time = Integer.parseInt(keyValue) * 60 * 1000;

                    Date afterDate = new Date(new Date().getTime() - time);
                    if (!addTime.after(afterDate)) {
                        //订单超时了,改变订单状态为系统自动取消
                        order.setOrderStatus((short) 103);

                        orderMapper.updateByPrimaryKeySelective(order);

                    } else {

                        unpaid++;
                    }
                }

                //判断是否处于待发货状态
                if (201 == order.getOrderStatus()) unship++;

                //判断是否处于待收货状态
                if (301 == order.getOrderStatus()) {
                    //发货时间
                    Date shipTime = order.getShipTime();
                    //设置超时时间
                    MarketSystem system = systemMapper.selectByPrimaryKey(4);
                    // 获得超时时间(天)
                    String keyValue = system.getKeyValue();

                    //将超时时间转换为天
                    long time = Integer.parseInt(keyValue) * 60 * 1000 * 60 * 24;

                    Date afterDate = new Date(new Date().getTime() - time);
                    if (!shipTime.after(afterDate)) {
                        //订单超时了,改变订单状态为系统自动收货
                        order.setOrderStatus((short) 402);
                        order.setConfirmTime(new Date());
                        orderMapper.updateByPrimaryKeySelective(order);

                    } else {

                        unrecv++;
                    }
                }


                //判断是否处于待评价状态
                if (401 == order.getOrderStatus() || 402 == order.getOrderStatus()) {

                    MarketOrderGoodsExample orderGoodsExample = new MarketOrderGoodsExample();
                    MarketOrderGoodsExample.Criteria orderGoodsCriteria = orderGoodsExample.createCriteria();
                    orderGoodsCriteria.andOrderIdEqualTo(order.getId());
                    List<MarketOrderGoods> orderGoods = orderGoodsMapper.selectByExample(orderGoodsExample);
                    for (MarketOrderGoods orderGood : orderGoods) {

                        Integer comment = orderGood.getComment();
                        if (0 == comment) {
                            //收货时间
                            Date confirmTime = order.getConfirmTime();
                            //设置超时时间
                            MarketSystem system = systemMapper.selectByPrimaryKey(11);
                            // 获得超时时间(天)
                            String keyValue = system.getKeyValue();

                            //将超时时间转换为天
                            long time = Integer.parseInt(keyValue) * 60 * 1000 * 60 * 24;

                            Date afterDate = new Date(new Date().getTime() - time);
                            if (!confirmTime.after(afterDate)) {
                                //订单超时了,不允许评论
                                orderGood.setComment(-1);

                                orderGoodsMapper.updateByPrimaryKeySelective(orderGood);
                                order.setComments((short) (order.getComments() - 1));

                            } else {

                                uncomment++;
                            }
                        }
                    }
                }
            }
        }

//        返回订单状态
        return new OrderStateVo(unrecv, uncomment, unpaid, unship);
    }

}
